var ClubRecordItem = Item.extend({
    _ccsFileName: res.ClubRecordItem_json,
    _recordInfo: null,
    _normalStylePlayerNum: 4,
    initItem: function () {
        this._nodeList['btn_share'].setVisible(false);
    },

    onEnter: function () {
        Item.prototype.onEnter.call(this);
        this._initAllListeners();
    },
    _initClickListeners: function () {
        this._clickListeners = {
            'btn_details': '_cliclDetails',
            'btn_share': '_cliclShare',
            'marked0': '_cliclMarked',
            'marked1': '_cliclMarked',
        }
    },

    /**
     * 监听亲友圈消息
     * @private
     */
    _initEventListeners: function () {
        this._eventListeners = {};
        //监听 消息 事件
        this._eventListeners[ClubRecordCode.ClubRecordMarked] = '_clubMaredHandler';
    },


    _cliclDetails: function () {
        if (this._recordInfo) {
            showWindow('RecordDetailsWindow', this._recordInfo);
        }
    },

    _cliclShare: function () {

    },

    _cliclMarked: function (target) {
        clubMsgManager.requestMarkedReocrd(this._recordInfo['club_id'], this._recordInfo['agent_id'], this._recordInfo['map_id'], 0, this._recordInfo['id'], timestamp2time(this._recordInfo['time_end'] / 1000));
    },

    _clubMaredHandler: function (room_rid) {
        // if (room_rid && room_rid == this._recordInfo['id']) {
        //     this._nodeList['marked0'].setVisible(!this._nodeList['marked0'].isVisible());
        //     this._nodeList['marked1'].setVisible(!this._nodeList['marked1'].isVisible());
        // }
    },

    /**
     *
     * @param idx
     * @param recordInfo   战绩信息
     * @param recordType   战绩类型0是全部 1是大赢家
     */
    refreshData: function (idx, recordInfo, recordType) {
        if (recordInfo == 0) {
            this.setVisible(false);
            return;
        }
        this.setVisible(true);
        this._recordInfo = recordInfo;
        var playerNum = recordInfo['players'].length || 0;
        if (playerNum > this._normalStylePlayerNum) {
            this._refreshMaxPlayerNode(idx, recordInfo, recordType);
        } else {
            this._refreshMinPlayerNode(idx, recordInfo, recordType);
        }
        this._nodeList['minPlayerNode'].setVisible(!(playerNum > this._normalStylePlayerNum));
        this._nodeList['maxPlayerNode'].setVisible((playerNum > this._normalStylePlayerNum));


        this._nodeList['wanFaName'].setString(ClubDataManager.getWanfaInfo2WanfaName(recordInfo));
        this._nodeList['roomId'].setString('房号:' + recordInfo['room_id']);
        this._nodeList['round'].setString('局数:' + recordInfo['round_current'] + '/' + recordInfo['round_total']);
        this._nodeList['playerCount'].setString('人数:' + playerNum);
        this._nodeList['createTime'].setString(timestamp2time(recordInfo['time_end'] / 1000));
        this._nodeList['marked0'].setVisible(recordInfo['marked'] == 0);
        this._nodeList['marked1'].setVisible(recordInfo['marked'] == 1);
    },


    _refreshMinPlayerNode: function (idx, recordInfo, recordType) {
        var players = recordInfo['players'] || [];
        var winners = recordInfo['winners'].split(',');
        var redColor = cc.color('#CD5F00');
        var greenColor = cc.color('#08AC07');

        for (var i = 0; i < 4; i++) {
            if (this._nodeList['minPlayerNode.head' + i] && cc.sys.isObjectValid(this._nodeList['minPlayerNode.head' + i])) {
                this._nodeList['minPlayerNode.head' + i].setVisible(false);
            }
        }
        for (var i = 0; i < players.length; i++) {
            var playerInfo = players[i];
            if (this._nodeList['minPlayerNode.head' + i] == undefined || !cc.sys.isObjectValid(this._nodeList['minPlayerNode.head' + i])) {
                this._nodeList['minPlayerNode.head' + i] = duplicateNode(this._nodeList['minPlayerNode.head0']);
                this._nodeList['minPlayerNode'].addChild(this._nodeList['minPlayerNode.head' + i]);
                this._nodeList['minPlayerNode.head' + i].x = this._nodeList['minPlayerNode.head0'].x + i * 190;
            }

            var head = this._nodeList['minPlayerNode.head' + i].getChildByName('head');
            var playerName = this._nodeList['minPlayerNode.head' + i].getChildByName('playerName');
            var playerId = this._nodeList['minPlayerNode.head' + i].getChildByName('playerId');
            var playerScore = this._nodeList['minPlayerNode.head' + i].getChildByName('playerScore');
            var icon_win = this._nodeList['minPlayerNode.head' + i].getChildByName('icon_win');

            loadImageToSprite(playerInfo['h'], head);
            playerName.setString(ellipsisStr(playerInfo['n'], ClubDataManager.NirckNameSize));
            playerId.setString('ID:' + playerInfo['i']);
            playerScore.setString(playerInfo['s']);
            if (playerInfo['s'] >= 0) {
                playerScore.setTextColor(redColor);
            } else {
                playerScore.setTextColor(greenColor);
            }
            icon_win.setVisible((winners.indexOf(playerInfo['i'].toString()) >= 0));
            this._nodeList['minPlayerNode.head' + i].setVisible(true);
        }
        this._nodeList['btn_details'].setVisible(false);
        this._nodeList['btn_share'].y = 68;
    },

    _refreshMaxPlayerNode: function (idx, recordInfo, recordType) {
        var players = recordInfo['players'] || [];
        var winners = recordInfo['winners'].split(',');
        var redColor = cc.color('#CD5F00');
        var greenColor = cc.color('#08AC07');

        for (var i = 0; i < 9; i++) {
            if (this._nodeList['maxPlayerNode.head' + i] && cc.sys.isObjectValid(this._nodeList['maxPlayerNode.head' + i])) {
                this._nodeList['maxPlayerNode.head' + i].setVisible(false);
            }
        }
        for (var i = 0; i < players.length; i++) {
            var playerInfo = players[i];
            if (this._nodeList['maxPlayerNode.head' + i] == undefined || !cc.sys.isObjectValid(this._nodeList['maxPlayerNode.head' + i])) {
                this._nodeList['maxPlayerNode.head' + i] = duplicateNode(this._nodeList['maxPlayerNode.head0']);
                this._nodeList['maxPlayerNode'].addChild(this._nodeList['maxPlayerNode.head' + i]);
                this._nodeList['maxPlayerNode.head' + i].x = this._nodeList['maxPlayerNode.head0'].x + i * 80;
            }

            var head = this._nodeList['maxPlayerNode.head' + i].getChildByName('head');
            var playerScore = this._nodeList['maxPlayerNode.head' + i].getChildByName('playerScore');
            var icon_win = this._nodeList['maxPlayerNode.head' + i].getChildByName('icon_win');

            loadImageToSprite(playerInfo['h'], head);
            playerScore.setString(playerInfo['s']);
            if (playerInfo['s'] >= 0) {
                playerScore.setTextColor(redColor);
            } else {
                playerScore.setTextColor(greenColor);
            }
            icon_win.setVisible((winners.indexOf(playerInfo['i'].toString()) >= 0));
            this._nodeList['maxPlayerNode.head' + i].setVisible(true);
        }

        this._nodeList['btn_details'].setVisible(true);
        this._nodeList['btn_share'].y = 37;
    },
    getRecordInfo: function () {
        return this._recordInfo;
    },
});